<?php
/**
 * Created for customized address.
 * User: Adams
 * Date: 14-7-22
 * Time: 下午4:39
 */ 
class Silk_Chinaarea_Helper_Data extends Mage_Directory_Helper_Data {

    public  $_cityJson="";
    /**
     * Retrieve city data json
     *
     */
    public function getCityJson()
    {
        return $this->getCityJsonByStore();
    }

    /**
     * Retrieve citys data json
     *
     * @param int|null $storeId
     * @return array()
     */
    public function getCityJsonByStore($storeId = null)
    {
        Varien_Profiler::start('TEST: '.__METHOD__);
        if (!$this->_cityJson) {
            $store = $this->_app->getStore($storeId);
            $cacheKey = 'DIRECTORY_CITY_JSON_STORE' . (string)$store->getId();
            if ($this->_app->useCache('config')) {
                $json = $this->_app->loadCache($cacheKey);
            }
            if (empty($json)) {
                $citys = $this->_getCities($storeId);
                $helper = $this->_factory->getHelper('core');
                $json = $helper->jsonEncode($citys);

                if ($this->_app->useCache('config')) {
                    $this->_app->saveCache($json, $cacheKey, array('config'));
                }
            }
            $this->_cityJson = $json;
        }

        Varien_Profiler::stop('TEST: ' . __METHOD__);
   
        return $this->_cityJson;
    }
    protected function _getAreas($city_id)
    {
      
         $areaModel = $this->_factory->getModel('chinaarea/area');
         $collection = $areaModel->getResourceCollection()->addFieldToFilter("city_id",$city_id)
            ->load();
           
         $areas = array();            
        foreach ($collection as $area){
            if (!$area->getAreaId()) {
                continue;
            }
            $areas[$area->getAreaId()]=$this->__($area->getDefaultName());
        }
        return $areas; 
    }

    /**
     * Get cities for specific region
     * @param string $storeId
     * @return array|null
     */
    protected function _getCities($storeId)
    {
        $cities = array();
        $cityModel = $this->_factory->getModel('chinaarea/city');
        $collection = $cityModel->getResourceCollection()
            ->load();
        foreach ($collection as $city) {
            if (!$city->getCityId()) {
                continue;
            }
            $cityitem = array();
            $cityitem['id']=$city->getCityId();
            $cityitem['name']=$this->__($city->getDefaultName());
            $cityitem['area']=$this->_getAreas($city->getCityId());

            $cities[$city->getRegionId()][]= $cityitem;
        }
        return $cities;
    }

    /**
     * Get Regions for specific Countries
     * @param string $storeId
     * @return array|null
     */
    protected function _getRegions($storeId)
    {
        $countryIds = array();

        $countryCollection = $this->getCountryCollection()->loadByStore($storeId);
        foreach ($countryCollection as $country) {
            $countryIds[] = $country->getCountryId();
        }

        /** @var $regionModel Mage_Directory_Model_Region */
        $regionModel = $this->_factory->getModel('directory/region');
        /** @var $collection Mage_Directory_Model_Resource_Region_Collection */
        $collection = $regionModel->getResourceCollection()
            ->addCountryFilter($countryIds)
            ->load();

        $regions = array(
            'config' => array(
                'show_all_regions' => $this->getShowNonRequiredState(),
                'regions_required' => $this->getCountriesWithStatesRequired()
            )
        );
        foreach ($collection as $region) {
            if (!$region->getRegionId()) {
                continue;
            }
            $regions[$region->getCountryId()][$region->getAreaId()] = array(
                'code' => $region->getCode(),
                'name' => $this->__($region->getName())
            );
        }
        return $regions;
    }
}